<!--MAIN HEADER-->
<?php

/*
 * This file is the header for each page.
 * 
 * Created by: Julian Alfonso Ponce
 * Modified by: Peter Agno Jr.
 * Date modified: September 25, 2011
 * 
 * In parameters : 
 * Out parameters : 
 */

    include('siteConfig.php'); 
    $userId = $_SESSION['loggedUserId'];
    $thePermissions = array();
    
    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // Identify if the menu bar will be displayed
    function checkPermission ( $thePermissions, $userId ) {
        $filterPermission = "";
        $flagFirst = true;
        
        foreach ( $thePermissions as $aPermission ) {
            if ( $flagFirst == true ) {
                $filterPermission = $filterPermission . "SP.permissionId = '".$aPermission."'";
                $flagFirst = false;
            }
            else {
                $filterPermission = $filterPermission . "OR SP.permissionId = '".$aPermission."'";
            }
        }
        
        // Check if the user has this permission
        $query =
            "
                SELECT ADP.userId as userId, DPS.securityGroupId as securityGroupId, SP.permissionId as permissionId
                FROM Account_Dept_Pos as ADP, Dept_Pos_SecurityGroup AS DPS, SecurityGroup_Permission as SP
                WHERE ADP.userId = '$userId' AND ADP.deptPosId = DPS.deptPosId AND DPS.securityGroupId = SP.securityGroupId AND ( ".$filterPermission." )
            ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_num_rows($result) > 0 )
            $show = true;
        else
            $show = false;
        
        // Free result
        mysql_free_result($result);
        
        return $show;
    }
?>
<table class="headerTable">
    <tr>
        <td colspan="2">
            <img src="images/gr_header.png" width="100%" height="100%" alt="Green Route"/>
        </td>
    </tr>
    <tr>
        <td class="headerUserInfo">
            Hi, 
            <?php        
            if( $_SESSION['firstName'] != NULL )
                echo $_SESSION['firstName']." ";

//            if( $_SESSION['middleName'] != NULL )
//                echo $_SESSION['middleName']." ";

            echo $_SESSION['lastName']." ";
            ?>
            
            | 
            <a href="logout.php" class="logoutLink">Logout</a>
        </td>
        <td class="headerSiteName">
            
        </td>
    </tr>
</table>


<!--NAVIGATION BAR -->
<div id="nav">
    <ul id="primary-nav" class="menuList">
        
        <?php
        // Display Dashboard
        $thePermissions = array('VIEWMAIN', 'VIEWATTCH');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
            ?>
            <li id="dashboard-nav" class="menubar"><a href="dashboard.php" title="Dashboard">Dashboard</a>
            </li>
            <?php
        }
        ?>
            
        <?php
        // Display Route Main Document
        $thePermissions = array('ROUTEMAIN');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
            ?>
            <li id="routeMainDoc-nav" class="menubar"><a href="tagDocument.php?documentType=MainDocument" title="Route Document">Route Document</a>
            </li>
            <?php
        }
        ?>
            
        <?php
        // Display Send Subdocument
        $thePermissions = array('SENDSUBDOC');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
            ?>
            <li id="sendSubdoc-nav" class="menubar"><a href="tagDocument.php?documentType=Attachment" title="Send Document">Send Document</a>
            </li>
            <?php
        }
        ?>
            
        <?php
        // Display Track Main Document
        $thePermissions = array('VIEWMAIN');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
            ?>
            <li id="trackMainDoc-nav" class="menubar"><a href="trackMainDocPage.php" title="Track Document">Track Document</a>
            </li>
            <?php
        }
        ?>
            
        <?php
        // Display Search
        $thePermissions = array('VIEWMAIN', 'VIEWATTCH');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
            ?>
            <li id="search-nav" class="menubar"><a href="javascript:void(0)" title="Search">Search <img src="images/navbar/green_arrow_down.png" alt=""/></a>
                <ul>
                    <li id="advSearch-nav" class="searchSubMenu"><a href="search.php?documentEvent=All" title="Advance Search">Advance Search</a>
                    </li>
                    <li id="approved-nav" class="searchSubMenu"><a href="search.php?documentEvent=Approved" title="Approved">Approved</a>
                    </li>
                    <li id="archived-nav" class="searchSubMenu"><a href="search.php?documentEvent=Archived" title="Archived">Archived</a>
                    </li>
                    <li id="cancelled-nav" class="searchSubMenu"><a href="search.php?documentEvent=Cancelled" title="Cancelled">Cancelled</a>
                    </li>
                    <li id="rejected-nav" class="searchSubMenu"><a href="search.php?documentEvent=Rejected" title="Rejected">Rejected</a>
                    </li>
                    <li id="retrnForRevsn-nav" class="searchSubMenu"><a href="search.php?documentEvent=Returned" title="Returned for Revisions">Returned for Revisions</a>
                    </li>
                    <li id="tagged-nav" class="searchSubMenu"><a href="search.php?documentEvent=Tagged" title="Tagged">Routed</a>
                    </li>
                    <li id="sent-nav" class="searchSubMenu"><a href="search.php?documentEvent=Sent" title="Sent">Sent</a>
                    </li>
                </ul>
            </li>
            <?php
        }
        ?>
            
        <?php
        // Display Admin Tools
        $thePermissions = array('DEPTSTP', 'POSSTP', 'ACCSTP', 'DOCSTP', 'ROUTESTP');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
            ?>
            <li id="adminTools-nav" class="menubar"><a href="javascript:void(0)" title="Admin Tools">Admin Tools <img src="images/navbar/green_arrow_down.png" alt=""/></a> 
                <ul>
                    
                    <?php
                    // Display Security Group Setup
                    $thePermissions = array('SECGRPSTP');
                    if ( checkPermission( $thePermissions, $userId ) == true ) {
                        ?>
                        <li id="secGrpSetup-nav" class="adminToolsSubMenu"><a href="admSecGroupSetup.php" title="Security Group">Security Group</a>
                        </li>
                        <?php
                    }
                    ?>
                    
                    <?php
                    // Display Organization Setup
                    $thePermissions = array('DEPTSTP', 'POSSTP');
                    if ( checkPermission( $thePermissions, $userId ) == true ) {
                        ?>
                        <li id="orgSetup-nav" class="adminToolsSubMenu"><a href="javascript:void(0)" title="Organization">Organization <img src="images/navbar/green_arrow_right.png" alt=""/></a>
                            <ul>
                                
                                <?php
                                // Display Department Setup
                                $thePermissions = array('DEPTSTP');
                                if ( checkPermission( $thePermissions, $userId ) == true ) {
                                    ?>
                                    <li id="deptSetup-nav" class="adminToolsSubMenu"><a href="admDepartmentSetup.php" title="Department">Department</a>
                                    </li>
                                    <?php
                                }
                                ?>
                                    
                                <?php
                                // Display Position Setup
                                $thePermissions = array('POSSTP');
                                if ( checkPermission( $thePermissions, $userId ) == true ) {
                                    ?>
                                    <li id="posSetup-nav" class="adminToolsSubMenu"><a href="admDeptPosSetup.php" title="Position">Position</a>
                                    </li>
                                    <?php
                                }
                                ?>
                                
                            </ul>
                        </li>
                        <?php
                    }
                    ?>
                       
                    <?php
                    // Display Account Setup
                    $thePermissions = array('ACCSTP');
                    if ( checkPermission( $thePermissions, $userId ) == true ) {
                        ?>
                        <li id="accSetup-nav" class="adminToolsSubMenu"><a href="admAccountSetup.php" title="Account">Account</a>
                        </li>
                        <?php
                    }
                    ?>
                        
                    <?php
                    // Display Document Setup
                    $thePermissions = array('DOCSTP');
                    if ( checkPermission( $thePermissions, $userId ) == true ) {
                        ?>
                        <li id="docSetup-nav" class="adminToolsSubMenu"><a href="admDocumentSetup.php" title="Document">Document</a>
                        </li>
                        <?php
                    }
                    ?>
                        
                    <?php
                    // Display Route Setup
                    $thePermissions = array('ROUTESTP');
                    if ( checkPermission( $thePermissions, $userId ) == true ) {
                        ?>
                        <li id="routeSetup-nav" class="adminToolsSubMenu"><a href="admRouteSetup.php" title="Route">Route</a>
                        </li>
                        <?php
                    }
                    ?>

                </ul>
            </li>
            <?php
        }
        ?>
 
    </ul>
</div>